package edu.cse484.lab3.task2.getcontacts;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TextView;

public class GetContacts extends Activity {
	private static final String TAG = "GetContacts";
	
	private static final int GET_CONTACTS_REQUEST_CODE = 111;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent intent = new Intent(GetContacts.this, GetContactsRequest.class);
        startActivityForResult(intent, GET_CONTACTS_REQUEST_CODE);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	if (requestCode == GET_CONTACTS_REQUEST_CODE) {
    		if (resultCode == RESULT_OK) {
    			// Everything was good
    			String[] emailList = (String[]) data.getExtras().get("Email");
    			Log.d(TAG, "Found emailList: " + emailList.toString() + " (length: " +emailList.length + ")");

    			StringBuilder builder = new StringBuilder();
    			for (String email : emailList) {
    				Log.d(TAG, "Email Result: " + email);
    				builder.append(email);
    				builder.append("\n");
    			}
    			
    			ScrollView emailsScrollView = (ScrollView) findViewById(R.id.emails_scroll_view);
    			TextView emailTextView = new TextView(this);
    			emailTextView.setText(builder.toString());
    			emailsScrollView.addView(emailTextView);
    		} else {
    			Log.e(TAG, "NOT OK RESULT CODE: " + resultCode);
    		}
    	} else {
			Log.e(TAG, "UNKNOWN REQUEST CODE: " + requestCode);
    	}
    }
}